home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / textfile / faqs / msds_faq / part3 < prev    next >
Encoding:
Text File  |  1992-12-26  |  42.6 KB  |  914 lines

  1. Xref: bloom-picayune.mit.edu comp.os.msdos.programmer:18890 news.answers:4713
  2. Path: bloom-picayune.mit.edu!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!mcsun!uknet!doc.ic.ac.uk!agate!ames!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!ncoast!brown
  3. From: brown@NCoast.ORG (Stan Brown)
  4. Newsgroups: comp.os.msdos.programmer,news.answers
  5. Subject: comp.os.msdos.programmer FAQ part 3 of 4
  6. Message-ID: <msdos-faq.921220.3@NCoast.ORG>
  7. Date: 20 Dec 92 20:14:13 GMT
  8. Expires: Wed, 3 Feb 1993 20:14:13 GMT
  9. References: <msdos-faq.921220.1@NCoast.ORG>
  10. Followup-To: comp.os.msdos.programmer
  11. Organization: Oak Road Systems, Cleveland Ohio USA
  12. Lines: 898
  13. Approved: news-answers-request@MIT.Edu
  14. Supersedes: <msdos-faq.921205.3@NCoast.ORG>
  15.  
  16. Archive-name: msdos-programmer-faq/part3
  17. Last-modified: 20 December 1922
  18.  
  19.  
  20. (continued from part 2)         (no warranty on the code or information)
  21.  
  22. If the posting date is more than six weeks in the past, see instructions
  23. in part 4 of this list for how to get an updated copy.
  24.  
  25.             Copyright (C) 1992  Stan Brown, Oak Road Systems
  26.  
  27.  
  28. section 4.  Disks and files
  29. ===========================
  30.  
  31. Q401. What drive was the PC booted from?
  32.  
  33.     Under DOS 4.0 or later, load 3305 hex into AX; do an INT 21.  DL is
  34.     returned with an integer indicating the boot drive (1=A:, etc.).
  35.  
  36. Q402. How can I boot from drive b:?
  37.  
  38.     Download PD1:<MSDOS.DSKUTL>BOOT_B.ZIP (shareware) from Simtel.  The
  39.     included documentation says it works by writing a new boot sector on
  40.     a disk in your a: drive that redirects the boot to your b: drive.
  41.  
  42. Q403. Which real and virtual disk drives are valid?
  43.  
  44.     Use INT 21 function 29 (parse filename).  Point DS:SI at a null-
  45.     terminated ASCII string that contains the drive letter and a colon,
  46.     point ES:DI at a 37-byte dummy FCB buffer, set AX to 2900h, and do
  47.     an INT 21.  On return, AL is FF if the drive is invalid, something
  48.     else if the drive is valid.  RAM disks and SUBSTed drives are
  49.     considered valid.
  50.  
  51.     Unfortunately, the b: drive is considered valid even on a single-
  52.     diskette system.  You can check that special case by interrogating
  53.     the BIOS equipment byte at 0040:0010.  Bits 7-6 contain the one less
  54.     than the number of diskette drives, so if those bits are zero you
  55.     know that b: is an invalid drive even though function 29 says it's
  56.     valid.
  57.  
  58.     Following is some code originally posted by Doug Dougherty, with my
  59.     fix for the b: special case, tested only in Borland C++ 2.0 (in
  60.     the small model):
  61.  
  62.         #include <dos.h>
  63.         void drvlist(void)  {
  64.             char *s = "A:", fcb_buff[37];
  65.             int valid;
  66.             for (   ;  *s<='Z';  (*s)++) {
  67.                 _SI = (unsigned) s;
  68.                 _DI = (unsigned) fcb_buff;
  69.                 _ES = _DS;
  70.                 _AX = 0x2900;
  71.                 geninterrupt(0x21);
  72.                 valid = _AL != 0xFF;
  73.                 if (*s == 'B'  &&  valid) {
  74.                     char far *equipbyte = (char far *)0x00400010UL;
  75.                     valid = (*equipbyte & (3 << 6)) != 0;
  76.                 }
  77.                 printf("Drive '%s' is %sa valid drive.\n",
  78.                         s, valid ? "" : "not ");
  79.             }
  80.         }
  81.  
  82. Q404. How can I make my single floppy drive both a: and b:?
  83.  
  84.     Under any DOS since DOS 2.0, you can put the command
  85.  
  86.         assign b=a
  87.  
  88.     into your AUTOEXEC.BAT file.  Then, when you type "DIR B:" you'll no
  89.     longer get the annoying prompt to insert diskette B (and the even
  90.     more annoying prompt to insert A the next time you type "DIR A:").
  91.  
  92.     You may be wondering why anybody would want to do this.  Suppose you
  93.     use two different machines, maybe one at home and one at work.  One
  94.     of them has only a 3.5" diskette drive; the other machine has two
  95.     drives, and b: is the 3.5" one.  You're bound to type "dir b:" on
  96.     the first one, and get the nuisance message
  97.  
  98.         Insert diskette for drive B: and press any key when ready.
  99.  
  100.     But if you assign drive b: to point to a:, you avoid this problem.
  101.  
  102.     Caution:  there are a few commands, such as DISKCOPY, that will not
  103.     work right on ASSIGNed or SUBSTed drives.  See the DOS manual for
  104.     the full list.  Before typing one of those commands, be sure to turn
  105.     off the mapping by typing "assign" without arguments.
  106.  
  107.     The DOS 5.0 manual says that ASSIGN is obsolete, and recommends the
  108.     equivalent form of SUBST: "subst b: a:\".  Unfortunately, if this
  109.     command is executed when a: doesn't hold a diskette, the command
  110.     fails.  ASSIGN doesn't have this problem, so I must advise you to
  111.     disregard that particular bit of advice in the DOS manual.
  112.  
  113. Q405. Why won't my C program open a file with a path?
  114.  
  115.     You've probably got something like the following code:
  116.  
  117.         char *filename = "c:\foo\bar\mumble.dat";
  118.         . . .  fopen(filename, "r");
  119.  
  120.     The problem is that \f is a form feed, \b is a backspace, and \m is
  121.     m.  Whenever you want a backslash in a string constant in C, you
  122.     must use two backslashes:
  123.  
  124.         char *filename = "c:\\foo\\bar\\mumble.dat";
  125.  
  126.     This is a feature of every C compiler, because Dennis Ritchie
  127.     designed C this way.  It's a problem only on MS-DOS systems, because
  128.     only DOS (and Atari ST/TT running TOS, I'm told) uses the backslash
  129.     in directory paths.  But even in DOS this backslash convention
  130.     applies _only_ to string constants in your source code.  For file
  131.     and keyboard input at run time, \ is just a normal character, so
  132.     users of your program would type in file specs at run time the same
  133.     way as in DOS commands, with single backslashes.
  134.  
  135.     Another possibility is to code all paths in source programs with /
  136.     rather than \ characters:
  137.  
  138.         char *filename = "c:/foo/bar/mumble.dat";
  139.  
  140.     Ralf Brown writes that "All versions of the DOS kernel accept either
  141.     forward or backslashes as directory separators.  I tend to use this
  142.     form more frequently than backslashes since it is easier to type and
  143.     read."  This applies to DOS function calls (and therefore to calls
  144.     to the file library of every programming language), but not to DOS
  145.     commands.
  146.  
  147. Q406. How can I redirect printer output to a file?
  148.  
  149.     My personal favorite utility for this purpose is PRN2FILE from {PC
  150.     Magazine}, available from Simtel as PD1:<MSDOS.PRINTER>PRN2FILE.ARC,
  151.     or from garbo as prn2file.zip in /pc/printer.  ({PC Magazine} has
  152.     given copies away as part of its utilities disks, so you may already
  153.     have a copy.)
  154.  
  155.     Check the PD1:<MSDOS.PRINTER> directory at Simtel, or /pc/printer
  156.     at garbo, for lots of other printer-redirection utilities.
  157.  
  158. Q407. How can my program open more files than DOS's limit of 20?
  159.  
  160.     (This is a summary of an article Ralf Brown posted on 8 August 1992.)
  161.  
  162.     There are separate limits on files and file handles.  For example,
  163.     DOS opens three files but five file handles:  CON (stdin, stdout,
  164.     and stderr), AUX (stdaux), and PRN (stdprn).
  165.  
  166.     The limit in FILES= in CONFIG.SYS is a system-wide limit on files
  167.     opened by all programs (including the three that DOS opens and any
  168.     opened by TSRs); each process has a limit of 20 handles (including
  169.     the five that DOS opens).  Example:  CONFIG.SYS has FILES=40.  Then
  170.     program #1 will be able to open 15 file handles.  Assuming that the
  171.     program actually does open 15 handles pointing to 15 different
  172.     files, other programs could still open a total of 22 files (40-3-15
  173.     = 22), though no one program could open more than 15 file handles.
  174.  
  175.     If you're running DOS 3.3 or later, you can increase the per-process
  176.     limit of 20 file handles by a call to INT 21 function 67, Set Handle
  177.     Count.  Your program is still limited by the system-wide limit on
  178.     open files, so you may also need to increase the FILES= value in
  179.     your CONFIG.SYS file (and reboot).  The run-time library that you're
  180.     using may have a fixed-size table of file handles, so you may also
  181.     need to get source code for the module that contains the table,
  182.     increase the table size, and recompile it.
  183.  
  184. Q408. How can I read, create, change, or delete the volume label?
  185.  
  186.     In DOS 5.0 (and, I believe, in 4.0 as well), there are actually two
  187.     volume labels: one, the traditional one, is an entry in the root
  188.     directory of the disk; and the other is in the boot record along
  189.     with the serial number (see next Q).  The DIR and VOL commands
  190.     report the traditional label; the LABEL command reports the
  191.     traditional one but changes both of them.
  192.  
  193.     In DOS 4.0 and later, use INT 21 function 69 to access the boot
  194.     record's serial number and volume label together; see the next Q.
  195.  
  196.     Assume that by "volume label" you mean the traditional one, the one
  197.     that DIR and VOL display.  Though it's a directory entry in the root
  198.     directory, you can't change it using the newer DOS file-access
  199.     functions (3C, 41, 43); instead, use the old FCB-oriented directory
  200.     functions.  Specifically, you need to allocate a 64-byte buffer and
  201.     a 41- byte extended FCB (file control block).  Call INT 21 AH=1A to
  202.     find out whether there is a volume label.  If there is, AL returns 0
  203.     and you can change the label using DOS function 17 or delete it
  204.     using DOS function 13.  If there's no volume label, function 1A will
  205.     return FF and you can create a label via function 16.  Important
  206.     points to notice are that ? wildcards are allowed but * are not; the
  207.     volume label must be space filled not null terminated.
  208.  
  209.     The following MSC 7.0 code worked for me in DOS 5.0; the functions
  210.     it uses have been around since DOS 2.0.  The function parameter is 0
  211.     for the current disk, 1 for a:, 2 for b:, etc.  It doesn't matter
  212.     what your current directory is; these functions always search the
  213.     root directory for volume labels.  (I didn't try to change the
  214.     volume label of any networked drives.)
  215.  
  216.     // Requires DOS.H, STDIO.H, STRING.H
  217.     void vollabel(unsigned char drivenum) {
  218.         static unsigned char extfcb[41], dta[64], status, *newlabel;
  219.         int chars_got = 0;
  220.         #define DOS(buff,func) __asm { __asm mov dx,offset buff \
  221.             __asm mov ax,seg buff  __asm push ds  __asm mov ds,ax \
  222.             __asm mov ah,func  __asm int 21h  __asm pop ds \
  223.             __asm mov status,al }
  224.         #define getlabel(buff,prompt) newlabel = buff;  \
  225.             memset(newlabel,' ',11);  printf(prompt);   \
  226.             scanf("%11[^\n]%n", newlabel, &chars_got);  \
  227.             if (chars_got < 11) newlabel[chars_got] = ' ';
  228.  
  229.         // Set up the 64-byte transfer area used by function 1A.
  230.         DOS(dta, 1Ah)
  231.         // Set up an extended FCB and search for the volume label.
  232.         memset(extfcb, 0, sizeof extfcb);
  233.         extfcb[0] = 0xFF;             // denotes extended FCB
  234.         extfcb[6] = 8;                // volume-label attribute bit
  235.         extfcb[7] = drivenum;         // 1=A, 2=B, etc.; 0=current drive
  236.         memset(&extfcb[8], '?', 11);  // wildcard *.*
  237.         DOS(extfcb,11h)
  238.         if (status == 0) {            // DTA contains volume label's FCB
  239.             printf("volume label is %11.11s\n", &dta[8]);
  240.             getlabel(&dta[0x18], "new label (\"delete\" to delete): ");
  241.             if (chars_got == 0)
  242.                 printf("label not changed\n");
  243.             else if (strncmp(newlabel,"delete     ",11) == 0) {
  244.                 DOS(dta,13h)
  245.                 printf(status ? "label failed\n" : "label deleted\n");
  246.             }
  247.             else {                    // user wants to change label
  248.                 DOS(dta,17h)
  249.                 printf(status ? "label failed\n" : "label changed\n");
  250.             }
  251.         }
  252.         else {                        // no volume label was found
  253.             printf("disk has no volume label.\n");
  254.             getlabel(&extfcb[8], "new label (<Enter> for none): ");
  255.             if (chars_got > 0) {
  256.                 DOS(extfcb,16h)
  257.                 printf(status ? "label failed\n" : "label created\n");
  258.             }
  259.         }
  260.     }   // end function vollabel
  261.  
  262. Q409. How can I get the disk serial number?
  263.  
  264.     Use INT 21.  AX=6900 gets the serial number; AX=6901 sets it.  See
  265.     Ralf Brown's interrupt list, or page 496 of the July 1992 {PC
  266.     Magazine}, for details.
  267.  
  268.     This function also gets and sets the volume label, but it's the
  269.     volume label in the boot record, not the volume label that a DIR
  270.     command displays.  See the preceding Q.
  271.  
  272. Q410. What's the format of .OBJ, .EXE., .COM files?
  273.  
  274.     Please see section 2, "Compile and link".
  275.  
  276.  
  277. section 5. Serial ports (COM ports)
  278. ===================================
  279.  
  280. Q501. How do I set my machine up to use COM3 and COM4?
  281.  
  282.     Unless your machine is fairly old, it's probably already set up.
  283.     After installing the board that contains the extra COM port(s),
  284.     check the I/O addresses in word 0040:0004 or 0040:0006.  (In DEBUG,
  285.     type "D 40:4 L4" and remember that every word is displayed low
  286.     byte first, so if you see "03 56" the word is 5603.)  If those
  287.     addresses are nonzero, your PC is ready to use the ports and you
  288.     don't need the rest of this answer.
  289.  
  290.     If the I/O address words in the 0040 segment are zero after you've
  291.     installed the I/O board, you need some code to store these values
  292.     into the BIOS data segment:
  293.  
  294.         0040:0004  word  I/O address of COM3
  295.         0040:0006  word  I/O address of COM4
  296.         0040:0011  byte (bits 3-1): number of serial ports installed
  297.  
  298.     The documentation with your I/O board should tell you the port
  299.     addresses.  When you know the proper port addresses, you can add
  300.     code to your program to store them and the number of serial ports
  301.     into the BIOS data area before you open communications.  Or you can
  302.     use DEBUG to create a little program to include in your AUTOEXEC.BAT
  303.     file, using this script:
  304.  
  305.             n SET_ADDR.COM      <--- or a different name ending in .COM
  306.             a 100
  307.             mov  AX,0040
  308.             mov  DS,AX
  309.             mov  wo [0004],aaaa <--- replace aaaa with COM3 address or 0
  310.             mov  wo [0006],ffff <--- replace ffff with COM4 address or 0
  311.             and  by [0011],f1
  312.             or   by [0011],8    <--- use number of serial ports times 2
  313.             mov  AH,0
  314.             int  21
  315.                                 <--- this line must be blank
  316.             rCX
  317.             1f
  318.             rBX
  319.             0
  320.             w
  321.             q
  322.  
  323. Q502. How do I find the I/O address of a COM port?
  324.  
  325.     Look in the four words beginning at 0040:0000 for COM1 through COM4.
  326.     (The DEBUG command "D 40:0 L8" will do this.  Remember that words
  327.     are stored and displayed low byte first, so a word value of 03F8
  328.     will be displayed as F8 03.)  If the value is zero, that COM port is
  329.     not installed (or you've got an old BIOS; see the preceding Q).  If
  330.     the value is nonzero, it is the I/O address of the transmit/receive
  331.     register for the COM port.  Each COM port occupies eight consecutive
  332.     I/O addresses (though only seven are used by many chips).
  333.  
  334.     Here's some C code to find the I/O address:
  335.  
  336.         unsigned ptSel(unsigned comport) {
  337.             unsigned io_addr;
  338.             if (comport >= 1  &&  comport <= 4) {
  339.                 unsigned far *com_addr = (unsigned far *)0x00400000UL;
  340.                 io_addr = com_addr[comport-1];
  341.             }
  342.             else
  343.                 io_addr = 0;
  344.             return io_addr;
  345.         }
  346.  
  347. Q503. But aren't the COM ports always at I/O addresses 3F8, 2F8, 3E8,
  348.       and 2E8?
  349.  
  350.     The first two are usually right (though not always); the last two
  351.     are different on many machines.
  352.  
  353. Q504. How do I configure a COM port and use it to transmit data?
  354.  
  355.     After hearing several recommendations, I looked at Joe Campbell's {C
  356.     Programmer's Guide to Serial Communications}, ISBN 0-672-22584-0,
  357.     and agree that it is excellent.  He gives complete details on how
  358.     serial ports work, along with complete programs for doing polled or
  359.     interrupt-driver I/O.  The book is quite thick, and none of it looks
  360.     like filler.
  361.  
  362.     If Campbell's book is overkill for you, you'll find a good short
  363.     description of serial I/O in {DOS 5: A Developer's Guide}, ISBN
  364.     1-55851-177-6, by Al Williams.
  365.  
  366.     You may also want to look at an extended example in Borland's
  367.     TechFax TI445, part of PD1:<MSDOS.TURBO-C> at Simtel.  Though
  368.     written by Borland, much of it is applicable to other forms of C,
  369.     and it should give you ideas for other programming languages.
  370.  
  371. section 6. Other hardware questions and problems
  372. ================================================
  373.  
  374. Q601. Which 80x86 CPU is running my program?
  375.  
  376.     According to an article posted by Michael Davidson, Intel's approved
  377.     code for distinguishing among 8086, 80286, 80386, and 80486 and for
  378.     detecting the presence of an 80287 or 80387 is published in the
  379.     Intel's 486SX processor manual (order number 240950-001).  You can
  380.     download David Kirschbaum's improved version of this from Simtel as
  381.     PD1:<MSDOS.SYSUTL>CPUID593.ZIP.
  382.  
  383.     According to an article posted by its author, WCPU041.ZIP knows the
  384.     differences between DX and SX varieties of 386 and 486 chips, and
  385.     can also detect a math coprocessor.  It's in PD1:<MSDOS.SYSUTL> at
  386.     Simtel.
  387.  
  388. Q602. How can a C program send control codes to my printer?
  389.  
  390.     If you just fprintf(stdprn, ...), C will translate some of your
  391.     control codes.  The way around this is to reopen the printer in
  392.     binary mode:
  393.  
  394.         prn = fopen("PRN", "wb");
  395.  
  396.     You must use a different file handle because stdprn isn't an lvalue.
  397.     By the way, PRN or LPT1 must not be followed by a colon in DOS 5.0.
  398.  
  399.     There's one special case, Ctrl-Z (ASCII 26), the DOS end-of-file
  400.     character.  If you try to send an ASCII 26 to your printer, DOS
  401.     simply ignores it.  To get around this, you need to reset the
  402.     printer from "cooked" to "raw" mode.  Microsoft C users must use int
  403.     21 function 44, "get/set device information".  Turbo C and Borland
  404.     C++ users can use ioctl to accomplish the same thing:
  405.  
  406.         ioctl(fileno(prn), 1, ioctl(fileno(prn),0) & 0xFF | 0x20, 0);
  407.  
  408.     An alternative approach is simply to write the printer output into a
  409.     disk file, then copy the file to the printer with the /B switch.
  410.  
  411.     A third approach is to bypass DOS functions entirely and use the
  412.     BIOS printer functions at INT 17.  If you also fprintf(stdprn,...)
  413.     in the same program, you'll need to use fflush( ) to synchronize
  414.     fprintf( )'s buffered output with the BIOS's unbuffered.
  415.  
  416.     By the way, if you've opened the printer in binary mode from a C
  417.     program, remember that outgoing \n won't be translated to carriage
  418.     return/line feed.  Depending on your printer, you may need to send
  419.     explicit \n\r sequences.
  420.  
  421. Q603. How can I redirect printer output to a file?
  422.  
  423.     Please see section 4, "Disks and files", for the answer.
  424.  
  425. Q604. Which video adapter is installed?
  426.  
  427.     The technique below should work if your BIOS is not too old.  It
  428.     uses three functions from INT 10, the BIOS video interrupt.  (If
  429.     you're using a Borland language, you may not have to do this the
  430.     hard way.  Look for a function called DetectGraph or something
  431.     similar.)
  432.  
  433.     Set AH=12h, AL=0, BL=32h; INT 10h.  If AL is 12h, you have a VGA.
  434.     If not, set AH=12h, BL=10h; INT 10h.  If BL is 0,1,2,3, you have an
  435.     EGA with 64,128,192,256K memory.  If not, set AH=0Fh; INT 10h.  If
  436.     AL is 7, you have an MDA (original monochrome adapter) or Hercules;
  437.     if not, you have a CGA.
  438.  
  439.     I've tested this for my VGA and got the right answer; but I can't
  440.     test it for the other equipment types.  Please let me know by email
  441.     at brown@ncoast.org if your results vary.
  442.  
  443. Q605. How do I switch to 43- or 50-line mode?
  444.  
  445.     Download PD1:<MSDOS.SCREEN>VIDMODE.ZIP from Simtel or one of the
  446.     mirror sites.  It contains .COM utilities and .ASM source code.
  447.  
  448. Q606. How can I find the Microsoft mouse position and button status?
  449.  
  450.     Use INT 33 function 3, described in Ralf Brown's interrupt list.
  451.  
  452.     The Windows manual says that the Logitech mouse is compatible with
  453.     the Microsoft one, so I assume the interrupt will work the same.
  454.  
  455.     Also, see the directory PD1:<MSDOS.MOUSE> at Simtel.
  456.  
  457. Q607. How can I access a specific address in the PC's memory?
  458.  
  459.     First check the library that came with your compiler.  Many vendors
  460.     have some variant of peek and poke functions; in Turbo Pascal use
  461.     the pseudo-arrays Mem, MemW, and MemL.  As an alternative, you can
  462.     construct a far pointer:  use Ptr in Turbo Pascal, MK_FP in the
  463.     Turbo C family, and FP_OFF and FP_SEG in Microsoft C.
  464.  
  465.     Caution:  Turbo C and Turbo C++ also have FP_OFF and FP_SEG macros,
  466.     but they can't be used to construct a pointer.  In Borland C++ those
  467.     macros work the same as in Microsoft C, but MK_FP is easier to use.
  468.  
  469.     By the way, it's not useful to talk about "portable" ways to do
  470.     this.  Any operation that is tied to a specific memory address is
  471.     not likely to work on another kind of machine.
  472.  
  473. Q608. How can I read or write my PC's CMOS memory?
  474.  
  475.     There are a great many public-domain utilities that do this.  These
  476.     were available for download from Simtel as of 31 March 1992:
  477.  
  478.     PD1:<MSDOS.AT>
  479.     CMOS14.ZIP     5965  920817  Saves/restores CMOS to/from file
  480.     CMOSER11.ZIP  28323  910721  386/286 enhanced CMOS setup program
  481.     CMOSRAM.ZIP   76096  920214  Save AT/386/486 CMOS data to file and restore
  482.     ROM2.ARC      20497  900131  Save AT and 386 CMOS data to file and restore
  483.     SETUP21.ARC   24888  880613  Setup program which modifies CMOS RAM
  484.     VIEWCMOS.ARC  15374  900225  Display contents of AT CMOS RAM, w/C source
  485.  
  486.     At garbo, /pc/ts/tsutle17.zip contains a CMOS program to check and
  487.     display CMOS memory, but not to write to it.
  488.  
  489.     I have heard good reports of CMOS299.ZIP, available in the pc.dir
  490.     directory of cantva.canterbury.ac.nz [132.181.30.3].
  491.  
  492.     Of the above, my only experience is with CMOSRAM, which seems to
  493.     work fine.  It contains an excellent (and witty) .DOC file that
  494.     explains the hardware involved and gives specific recommendations
  495.     for preventing disaster or recovering from it.  It's $5 shareware.
  496.  
  497.     Robert Jourdain's {Programmer's Problem Solver for the IBM PC, XT,
  498.     and AT} has code for accessing the CMOS RAM, according to an article
  499.     posted in this newsgroup.
  500.  
  501. Q609. How can I access memory beyond 640K?
  502.  
  503.     I'm outside my expertise on this one, but in late 1992 Jamshid
  504.     Afshar (jamshid@emx.utexas.edu) kindly supplied the following, which
  505.     incorporates some corrections agreed with Duncan Murdoch (dmurdoch@
  506.     mast.queensu.ca).  If you have any corrections or comments, please
  507.     send them to both the above addresses.
  508.  
  509.     ...........................(begin quote)............................
  510.     1. Use XMS or EMS memory.  XMS is preferable in most cases, but
  511.     some machines won't provide it.  There are some libraries available
  512.     at Simtel to access XMS or EMS.  The disadvantage is that you
  513.     don't allocate the memory as you would with malloc() (or `new' in
  514.     C++).  I believe it also requires that you lock this memory when in
  515.     use.  This means your code is not easily ported to other (and
  516.     future) operating systems and that your code is more convoluted than
  517.     it would be under a "real" os.  The advantage is that the library
  518.     works with compilers since Turbo C 2.0 (I think) and that your
  519.     program will easily run on even 286s.
  520.  
  521.     2.  Program under MS Windows.  MS Windows functions as a 16-bit DOS
  522.     Extender (see #3).  Borland/Turbo C++ 3.x includes EasyWin [and
  523.     Microsoft C/C++ 7.0 has QuickWin --ed.] which is a library that
  524.     automatically lets you compile your current code using C/C++
  525.     standard input or <conio.h> into a MS Windows program so your code
  526.     can immediately allocate many MBs of memory (Windows enhanced mode
  527.     even does virtual memory).  The disadvantage is that like any 16-bit
  528.     Extender a single malloc() is restricted to 64K (unless you want to
  529.     mess with huge pointers in Windows).  Also, EasyWin's screen output
  530.     is significantly slower than a DOS character-mode program's and you
  531.     must of course run the program from Windows.
  532.  
  533.     3.  Use a 16-bit or 32-bit DOS Extender.  This is definitely the
  534.     best solution from the programmer's standpoint.  You just allocate
  535.     as much memory as you need using malloc() or 'new'.  A 16-bit
  536.     Extender still has 16-bit ints and restricts arrays to 64K, but a
  537.     32-bit Extender has 32-bits ints (which makes porting a lot of UNIX
  538.     code easier) so there are no 64K limits.  A 32-bit Extender requires
  539.     a 32-bit compiler and the program will not run on 286s.  Some
  540.     Extenders also do virtual memory.  Using an Extender doesn't require
  541.     source code changes and unlike option #1 your code is portable and
  542.     not obsolete in a few months.  Your options for this solution are:
  543.  
  544.     - Buy PharLap's 16-bit Extender that works with BC++ 3.0+ and MSC
  545.       (just requires a relink).  Note, the BC++ 3.1 upgrade came with
  546.       PharLap "lite".  Pharlap's 32-bit Extender works with 32-bit
  547.       compilers like [?]
  548.  
  549.     - Get the GNU (free,copylefted) gcc 2.x compiler which DJ Delorie
  550.       ported from UNIX and which uses his 32-bit Extender.  It supports
  551.       C and C++, but the Extender is VCPI which means neither the
  552.       compiler nor programs it produces will run in a DOS session under
  553.       Windows.  FTP to barnacle.erc.clarkson.edu and get
  554.       pub/msdos/djgpp/readme.
  555.  
  556.     - Get a 32-bit compiler or one that comes with a DOS Extender.
  557.       Zortech comes with 16-bit and a 32-bit Extenders (no debugger for
  558.       32-bit programs, but Flashtek sells one).  Watcom also makes a C
  559.       [and C++?] 32-bit compiler.  [If anyone else has products or plans
  560.       to announce, please let me know.]
  561.  
  562.     - Buy Borland Pascal 7.0.  It includes a 16 bit royalty-free DOS
  563.       extender using the same interface as MS Windows.  It functions
  564.       under a DPMI server like Windows or QDPMI from Quarterdeck, and
  565.       also provides its own server which you can distribute with your
  566.       programs.
  567.  
  568.     4.  This option doesn't really count since it's not a solution in
  569.     DOS, but you could switch to a full 32-bit operating system like
  570.     OS/2 2.0 or UNIX (or NT when it comes out).  I believe Win32 will
  571.     allow you to write 32-bit Windows programs.  [can someone fill me in
  572.     on what exactly Win32 is?]
  573.     ............................(end quote).............................
  574.  
  575.  
  576. section 7. Other software questions and problems
  577. ================================================
  578.  
  579. Q701. How can a program reboot my PC?
  580.  
  581.     You can generate a "cold" boot or a "warm" boot.  A cold boot is
  582.     the same as turning the power off and on; a warm boot is the same as
  583.     Ctrl-Alt-Del and skips the power-on self test.
  584.  
  585.     For a warm boot, store the hex value 1234 in the word at 0040:0072.
  586.     For a cold boot, store 0 in that word.  Then, if you want to live
  587.     dangerously, jump to address FFFF:0000.  Here's C code to do it:
  588.  
  589.         /* WARNING:  data loss possible */
  590.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  591.             void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  592.             unsigned far* type = (unsigned far*)0x00400072UL;
  593.             *type = (want_warm ? 0x1234 : 0);
  594.             (*boot)( );
  595.         }
  596.  
  597.     What's wrong with that method?  It will boot right away, without
  598.     closing files, flushing disk caches, etc.  If you boot without
  599.     flushing a write-behind disk cache (if one is running), you could
  600.     lose data or even trash your hard drive.
  601.  
  602.     There are two methods of signaling the cache to flush its buffers:
  603.     (1) simulate a keyboard Ctrl-Alt-Del in the keystroke translation
  604.     function of the BIOS (INT 15 function 4F), and (2) issue a disk
  605.     reset (DOS function 0D).  Most disk-cache programs hook one or both
  606.     of those interrupts, so if you use both methods you'll probably be
  607.     safe.
  608.  
  609.     When user code simulates a Ctrl-Alt-Del, one or more of the programs
  610.     that have hooked INT 15 function 4F can ask that the key be ignored by
  611.     clearing the carry flag.  For example, HyperDisk does this when it
  612.     has started but not finished a cache flush.  So if the carry flag
  613.     comes back cleared, the boot code has to wait a couple of cluck
  614.     ticks and then try again.  (None of this matters on older machines
  615.     whose BIOS can't support 101- or 102-key keyboards; see "What is the
  616.     SysRq key for?" in section 3, "Keyboard".)
  617.  
  618.     Here's C code that tries to signal the disk cache (if any) to flush:
  619.  
  620.         #include <dos.h>
  621.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  622.             union REGS reg;
  623.             void    (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  624.             unsigned far* boottype    =     (unsigned far*)0x00400072UL;
  625.             char     far* shiftstate  =         (char far*)0x00400017UL;
  626.             unsigned      ticks;
  627.             int           time_to_waste;
  628.             /* Simulate reception of Ctrl-Alt-Del: */
  629.             for (;;) {
  630.                 *shiftstate |= 0x0C;    /* turn on Ctrl & Alt */
  631.                 reg.x.ax = 0x4F53;      /* 0x53 = Del's scan code */
  632.                 reg.x.cflag = 1;        /* sentinel for ignoring key */
  633.                 int86(0x15, ®, ®);
  634.                 /* If carry flag is still set, we've finished. */
  635.                 if (reg.x.cflag)
  636.                     break;
  637.                 /* Else waste some time before trying again: */
  638.                 reg.h.ah = 0;
  639.                 int86(0x1A, ®, ®);/* system time into CX:DX */
  640.                 ticks = reg.x.dx;
  641.                 for (time_to_waste = 3;  time_to_waste > 0;  ) {
  642.                     reg.h.ah = 0;
  643.                     int86(0x1A, ®, ®);
  644.                     if (ticks != reg.x.dx)
  645.                         ticks = reg.x.dx , --time_to_waste;
  646.                 }
  647.             }
  648.             /* Issue a DOS disk reset request: */
  649.             reg.h.ah = 0x0D;
  650.             int86(0x21, ®, ®);
  651.             /* Set boot type and boot: */
  652.             *boottype = (want_warm ? 0x1234 : 0);
  653.             (*boot)( );
  654.         }
  655.  
  656. Q702. How can I time events with finer resolution than the system
  657.       clock's 55 ms (about 18 ticks a second)?
  658.  
  659.     The following files, among others, can be downloaded from Simtel:
  660.  
  661.     PD1:<MSDOS.AT>
  662.     ATIM.ARC       5946  881126  Precision program timing for AT
  663.  
  664.     PD1:<MSDOS.C>
  665.     MILLISEC.ZIP  37734  911205  MSC/asm src for millisecond res timing
  666.     MSCHRT3.ZIP   53708  910605  High-res timer toolbox for MSC 5.1
  667.     MSEC_12.ZIP    8484  920320  High-def millisec timer v1.2 (C,ASM)
  668.     ZTIMER11.ZIP  77625  920428  Microsecond timer for C, C++, ASM
  669.  
  670.     PD1:<MSDOS.TURBO-C>
  671.     TCHRT3.ZIP    53436  910606  High-res timer toolbox for Turbo C 2.0
  672.     TCTIMER.ARC   20087  891030  High-res timing of events for Turbo C
  673.  
  674.     PD1:<MSDOS.TURBOPAS>
  675.     BONUS507.ARC 150435  900205  [Turbo Pascal source: high-res timing]
  676.  
  677.     Pascal users can download source code in /pc/turbopas/bonus507.zip
  678.     at garbo.
  679.  
  680. Q703. How can I find the error level of the previous program?
  681.  
  682.     First, which previous program are you talking about?  If your
  683.     current program ran another one, when the child program ends its
  684.     error level is available to the program that spawned it.  Most
  685.     high-level languages provide a way to do this; for instance, in
  686.     Turbo Pascal it's Lo(DosExitCode) and the high byte gives the way in
  687.     which the child terminated.  In Microsoft C, the exit code of a
  688.     synchronous child process is the return value of the spawn-type
  689.     function that creates the process.
  690.  
  691.     If your language doesn't have a function to return the error code
  692.     of a child process, you can use INT 21 function 4D (get return
  693.     code).  By the way, this will tell you the child's exit code and the
  694.     manner of its ending (normal, Ctrl-C, critical error, or TSR).
  695.  
  696.     It's much trickier if the current program wants to get the error
  697.     level of the program that ran and finished before this one started.
  698.     G.A.Theall has published source and compiled code to do this; you
  699.     can download it from Simtel as PD1:<MSDOS.BATUTL>ERRLVL12.ZIP.  (The
  700.     code uses undocumented features in DOS 3.3 through 5.0.  Theall says
  701.     in the .DOC file that the values returned under 4DOS or other
  702.     replacements won't be right.)
  703.  
  704. Q704. How can a program set DOS environment variables?
  705.  
  706.     Program functions that read or write "the environment" typically
  707.     access only the program's copy of the environment.  What this Q
  708.     really wants to do is to modify the active environment, the one that
  709.     is affected by SET commands in batch files or at the DOS prompt.
  710.     You need to do some programming to find the active environment, and
  711.     that programming varies for different versions of DOS.
  712.  
  713.     A fairly well-written article in {PC Magazine} volume 8 number 20
  714.     (1989 Nov 28), pages 309-314, explains how to find the active
  715.     environment, and includes Pascal source code.  The article hints at
  716.     how to change the environment, and suggests creating paths longer
  717.     than 128 characters as one application.
  718.  
  719.     In searching Simtel for source code, I found many possibilities.  I
  720.     liked PD1:<MSDOS.SYSUTL>RBSETNV1.ZIP of the ones I looked at (not
  721.     all of them).  It includes some utilities to manipulate the environ-
  722.     ment, with source code in C.
  723.  
  724.     You can also use a call to INT 2E, Pass Command to Interpreter for
  725.     Execution; see Ralf Brown's interrupt list for details and cautions.
  726.  
  727. Q705. How can I change the switch character to - from /?
  728.  
  729.     Under DOS 5.0, you can't -- not completely, anyway.  INT 21 function
  730.     3700, get switch character, always returns a '/' (hex 2F) -- and the
  731.     DOS commands don't even call that function, but hard code '/' as the
  732.     switch character.
  733.  
  734.     Some history:  DOS used to let you change the switch character by
  735.     using SWITCHAR= in CONFIG.SYS or by calling DOS function 3701.  DOS
  736.     commands and other programs called DOS function 3700 to find out the
  737.     switch character.  If you changed the switch character to '-' (the
  738.     usual choice), you could then type "dir c:/c700 -p" rather than "dir
  739.     c:\c700 /p".  Under DOS 4.0, the DOS commands ignored the switch
  740.     character but functions 3700 and 3701 still worked and could be used
  741.     by other programs.  Under DOS 5.0, even those functions no longer
  742.     work, though all DOS functions still accept '/' or '\' in file
  743.     specs.
  744.  
  745.     You can reactivate the functions to get and set switchar by using
  746.     programs like SLASH.ZIP or the sample TSR called SWITCHAR in
  747.     AMISL091.ZIP (see "How can I write a TSR?", below.)  DOS commands
  748.     will still use the slash, but non-DOS programs that call DOS func-
  749.     tion 3700 will use your desired switch character.  (DOS replacements
  750.     like 4DOS may honor the switch character for internal commands.)
  751.  
  752.     Some readers may wonder why this is even an issue.  Making '-' the
  753.     switch character frees up the front slash to separate names in the
  754.     path part of a file spec.  This is easier for the ten-fingered to
  755.     type, and it's one less difference to remember for commuters between
  756.     DOS and Unix.  The switch character is the only issue, since all the
  757.     INT 21 functions accept '/' or '\' to separate directory names.
  758.  
  759. Q706. Why does my interrupt function behave strangely?
  760.  
  761.     Interrupt service routines can be tricky, because you have to do
  762.     some things differently from "normal" programs.  If you make a
  763.     mistake, debugging is a pain because the symptoms may not point at
  764.     what's wrong.  Your machine may lock up or behave erratically, or
  765.     just about anything else can happen.  Here are some things to look
  766.     for.  (See the next Q for general help before you have a problem.)
  767.  
  768.     First, did you fail to set up the registers at the start of your
  769.     routine?  When your routine begins executing, you can count on
  770.     having CS point to your code segment and SS:SP point to some valid
  771.     stack (of unknown length), and that's it.  In particular, an
  772.     interrupt service routine must set DS to DGROUP before accessing any
  773.     data in its data segments.  (If you're writing in a high-level
  774.     language, the compiler may generate this code for you automatically;
  775.     check your compiler manual.  For instance, in Borland and Microsoft
  776.     C, give your function the "interrupt" attribute.)
  777.  
  778.     Did you remember to turn off stack checking when compiling your
  779.     interrupt server and any functions it calls?  The stack during the
  780.     interrupt is not where the stack-checking code expects it to be.
  781.     (Caution:  Some third-party libraries have stack checking compiled
  782.     in, so you can't call them from your interrupt service routine.)
  783.  
  784.     Next, are you calling any DOS functions (INT 21, 25, or 26) in your
  785.     routine?  DOS is not re-entrant.  This means that if your interrupt
  786.     happens to be triggered while the CPU is executing a DOS function,
  787.     calling another DOS function will wreak havoc.  (Some DOS functions
  788.     are fully re-entrant, as noted in Ralf Brown's interrupt list.
  789.     Also, your program can test, in a way too complicated to present
  790.     here, when it's safe to call non-re-entrant DOS functions.  See INT
  791.     28 and functions 34, 5D06, 5D0B of INT 21; and consult {Undocumented
  792.     DOS} by Andrew Schulman.  Your program must read both the "InDOS
  793.     flag" and the "critical error flag".)
  794.  
  795.     Is a function in your language library causing trouble?  Does it
  796.     depend on some initializations done at program startup that is no
  797.     longer available when the interrupt executes?  Does it call DOS (see
  798.     preceding paragraph)?  For example, in both Borland and Microsoft C
  799.     the memory-allocation functions (malloc, etc..) and standard I/O
  800.     functions (scanf, printf) call DOS functions and also depend on
  801.     setups that they can't get at from inside an interrupt.  Many other
  802.     library functions have the same problem, so you can't use them
  803.     inside an interrupt function without special precautions.
  804.  
  805.     Is your routine simply taking too long?  This can be a problem if
  806.     you're hooking on to the timer interrupt, INT 1C or INT 8.  Since
  807.     that interrupt expects to be called 18.2 times a second, your
  808.     routine -- plus any others hooked to the same interrupts -- must
  809.     execute in less than 55 ms.  If they use even a substantial fraction
  810.     of that time, you'll see significant slowdowns of your foreground
  811.     program.  For a good writeup, download INTSHARE (from ni.funet.fi
  812.     in pub/msdos/simtel20/info or from Simtel in PD1:<MSDOS.INFO>).
  813.  
  814.     Did you forget to restore all registers at the end of your routine?
  815.  
  816.     Did you chain improperly to the original interrupt?  You need to
  817.     restore the stack to the way it was upon entry to your routine, then
  818.     do a far jump (not call) to the original interrupt service routine.
  819.     (The process is a little different in high-level languages.)
  820.  
  821. Q707. How can I write a TSR (terminate-stay-resident) utility?
  822.  
  823.     Several books can help you with this.
  824.  
  825.     - Ray Duncan's {Advanced MS-DOS}, ISBN 1-55615-157-8, gives a brief
  826.       checklist intended for experienced programmers.  The ISBN is for
  827.       the second edition, through DOS 4; but check to see whether the
  828.       DOS 5 version is available yet.
  829.  
  830.     - {DOS 5:  A Developer's Guide} by Al Williams, ISBN 1-55851-177-6,
  831.       goes into a little more detail, 90 pages worth!
  832.  
  833.     - Pascal programmers might look at {The Ultimate DOS Programmer's
  834.       Manual} by John Mueller and Wallace Wang, ISBN 0-8306-3534-3, for
  835.       an extended example in mixed Pascal and assembler.
  836.  
  837.     - For a pure assembler treatment, check Steven Holzner's {Advanced
  838.       Assembly Language}, ISBN 0-13-663014-6.  He has a book with the
  839.       same title out from Brady Press, but it's about half as long as
  840.       this one.
  841.  
  842.     - For C programmers, there's a chapter in Herbert Schildt's {The Art
  843.       of C:  Elegant Programming Solutions}.  I haven't seen the book,
  844.       but a posted article recommended it.
  845.  
  846.     At Simtel, check PD1:<MSDOS.ASMUTL>AMISL091.ZIP, which contains Ralf
  847.     Brown's assembly-language implementation of the Alternate Multiplex
  848.     Interrupt Specification, with utilities in C.  The spec itself is
  849.     PD1:<MSDOS.INFO>ALTMPX35.ZIP.  Both are also available at CS.CMU.EDU
  850.     [128.2.222.173] in /afs/cs/user/ralf/pub (change directory with a
  851.     single command and use lower-case filenames).
  852.  
  853.     You might want to download PD1:<MSDOS.ASMUTL>TEMPLATE.ZIP from
  854.     Simtel.  It's Douglas Boling's MASM template for a TSR.
  855.  
  856.     Finally, there are commercial products, of which TesSeRact (for
  857.     C-language TSRs) is one of the best known.
  858.  
  859. Q708. How can I write a device driver?
  860.  
  861.     Many books answer this in detail.  Among them are {Advanced MS-DOS}
  862.     and {DOS 5: A Developer's Guide}, cited in the preceding Q.
  863.     Michael Tischer's {PC System Programming}, ISBN 1-55755-036-0, has
  864.     an extensive treatment, as does Dettman and Kyle's {DOS Programmer's
  865.     Reference: 2d Edition}, ISBN 0-88022-458-4.  For a really in-depth
  866.     treatment, look for a specialized book like Robert Lai's {Writing
  867.     MS-DOS Device Drivers}, ISBN 0-201-13185-4.
  868.  
  869. Q709. What can I use to manage versions of software?
  870.  
  871.     In PD1:<MSDOS.PGMUTL> at Simtel you'll find RCS56DOS.ZIP.  I haven't
  872.     used it myself, but I understand this is a port of the Unix RCS
  873.     utility, and is no longer limited to one-character extensions on
  874.     filenames (so .CPP and .BAS are fine).
  875.  
  876. Q710. What's this "null pointer assignment" after my C program executes?
  877.  
  878.     Somewhere in your program, you assigned a value _through_ a pointer
  879.     without first assigning a value _to_ the pointer.  (This might have
  880.     been something like a strcpy or memcpy with a pointer as its first
  881.     argument, not necessarily an actual assignment statement.)  Your
  882.     program may look like it ran correctly, but if you get this message
  883.     you can be certain that there's a bug somewhere.
  884.  
  885.     Microsoft and Borland C, as part of their exit code (after a return
  886.     from your main function), check whether the location 0000 in your
  887.     data segment contains a different value from what you started with;
  888.     if so, they infer that you must have used an uninitialized pointer.
  889.  
  890.     To track down the problem, you can put exit( ) statements at various
  891.     spots in the program and narrow down where the uninitialized pointer
  892.     is being used by seeing which added exit( ) makes the null-pointer
  893.     message disappear.  Or, in the debugger, set a watch at location
  894.     0000 in your data segment, assuming you're in small or medium model.
  895.     (If data pointers are 32 bits, as in the compact and large models, a
  896.     null pointer will overwrite the interrupt vectors at 0000:0000 and
  897.     probably lock up your machine.)
  898.  
  899.     Under MSC/C++ 7.0, you can declare the undocumented library function
  900.  
  901.         extern _cdecl _nullcheck(void);
  902.  
  903.     and then sprinkle calls to _nullcheck( ) through your program at
  904.     regular intervals.
  905.  
  906.     Borland's TechFax document #TI726 discusses the null pointer
  907.     assignment from a Borland point of view.  Download file BCHELP10.ZIP
  908.     from PD1:<MSDOS.TURBO-C> at Simtel.
  909.  
  910. (continued in part 4)
  911. -- 
  912. Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
  913. Cleveland, Ohio, USA
  914.